package com.example.firstboot.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.firstboot.entity.TbSchedule;
import com.example.firstboot.entity.TbUser;
import com.example.firstboot.entity.TbWeeklyschedule;
import com.example.firstboot.service.*;
import com.example.firstboot.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/rosteringManage")
public class RosteringController {
    @Autowired
    private TbDocondutyService tbDocondutyService;
    @Autowired
    private TbUserService tbUserService;
    @Autowired
    private TbScheduleService tbScheduleService;
    @Autowired
    private TbWeeklyscheduleService tbWeeklyscheduleService;
    @Autowired
    private DocOndutyService docOndutyService;
    @RequestMapping("/rostering")
    @ResponseBody
    public JsonResult queryAllDoc() {
        List<TbUser> list = tbUserService.queryAllDoc();
        if (list.size() == 0) {
            return new JsonResult(-1, "请求失败，没有查询到医生信息！");
        } else {
            return new JsonResult(list, "请求成功");
        }
    }

    @RequestMapping("/fuzzyQuery")
    @ResponseBody
    public JsonResult fuzzyQuery(@RequestBody JSONObject jsonObject) {
        String account = String.valueOf(jsonObject.get("account"));
        String username = String.valueOf(jsonObject.get("username"));
        String department = String.valueOf(jsonObject.get("department"));
        List<TbUser> list = tbUserService.fuzzyQuery(account, username, department);
        if (list.size() == 0) {
            return new JsonResult(-1, "请求失败，没有查询到门诊医生信息！");
        } else {
            return new JsonResult(list, "请求成功");
        }
    }

    //获取某医生的一周中工作或休息
    @RequestMapping("/mondayoptions")
    @ResponseBody
    public JsonResult mondayoptions(@RequestBody JSONObject jsonObject) {
        String account = String.valueOf(jsonObject.get("account"));
        List<TbSchedule> list = tbScheduleService.queryMonday(account);
        if (list.size() == 0) {
            return new JsonResult(-1, "请求失败，没有查询到排班信息！");
        } else {
            System.out.println(new JsonResult(list, "请求成功"));
            return new JsonResult(list);
        }
    }

    @RequestMapping("/savemonday")
    @ResponseBody
    public JsonResult savemonday(@RequestBody JSONObject jsonObject) {
        System.out.println(jsonObject);
        String account = String.valueOf(jsonObject.get("account"));
        String monday = String.valueOf(jsonObject.get("monday"));
        int num = tbScheduleService.updatemonday(account, monday);
        if (num == 0) {
            return new JsonResult(-1, "请求失败！");
        } else {
            System.out.println(new JsonResult(num, "请求成功"));
            return new JsonResult(num);
        }
    }

    @RequestMapping("/savetuesday")
    @ResponseBody
    public JsonResult savetuesday(@RequestBody JSONObject jsonObject) {
        System.out.println(jsonObject);
        String account = String.valueOf(jsonObject.get("account"));
        String tuesday = String.valueOf(jsonObject.get("tuesday"));
        int num = tbScheduleService.updatetuesday(account, tuesday);
        if (num == 0) {
            return new JsonResult(-1, "请求失败！");
        } else {
            System.out.println(new JsonResult(num, "请求成功"));
            return new JsonResult(num);
        }
    }

    @RequestMapping("/savewednesday")
    @ResponseBody
    public JsonResult savewednesday(@RequestBody JSONObject jsonObject) {
        System.out.println(jsonObject);
        String account = String.valueOf(jsonObject.get("account"));
        String wednesday = String.valueOf(jsonObject.get("wednesday"));
        int num = tbScheduleService.updatewednesday(account, wednesday);
        if (num == 0) {
            return new JsonResult(-1, "请求失败！");
        } else {
            System.out.println(new JsonResult(num, "请求成功"));
            return new JsonResult(num);
        }
    }

    @RequestMapping("/savethursday")
    @ResponseBody
    public JsonResult savethursday(@RequestBody JSONObject jsonObject) {
        System.out.println(jsonObject);
        String account = String.valueOf(jsonObject.get("account"));
        String thursday = String.valueOf(jsonObject.get("thursday"));
        int num = tbScheduleService.updatethursday(account, thursday);
        if (num == 0) {
            return new JsonResult(-1, "请求失败！");
        } else {
            System.out.println(new JsonResult(num, "请求成功"));
            return new JsonResult(num);
        }
    }

    @RequestMapping("/savefriday")
    @ResponseBody
    public JsonResult savefriday(@RequestBody JSONObject jsonObject) {
        System.out.println(jsonObject);
        String account = String.valueOf(jsonObject.get("account"));
        String friday = String.valueOf(jsonObject.get("friday"));
        int num = tbScheduleService.updatefriday(account, friday);
        if (num == 0) {
            return new JsonResult(-1, "请求失败！");
        } else {
            System.out.println(new JsonResult(num, "请求成功"));
            return new JsonResult(num);
        }
    }

    @RequestMapping("/savesaturday")
    @ResponseBody
    public JsonResult savesaturday(@RequestBody JSONObject jsonObject) {
        System.out.println(jsonObject);
        String account = String.valueOf(jsonObject.get("account"));
        String saturday = String.valueOf(jsonObject.get("saturday"));
        int num = tbScheduleService.updatesaturday(account, saturday);
        if (num == 0) {
            return new JsonResult(-1, "请求失败！");
        } else {
            System.out.println(new JsonResult(num, "请求成功"));
            return new JsonResult(num);
        }
    }

    @RequestMapping("/savesunday")
    @ResponseBody
    public JsonResult savesunday(@RequestBody JSONObject jsonObject) {
        System.out.println(jsonObject);
        String account = String.valueOf(jsonObject.get("account"));
        String sunday = String.valueOf(jsonObject.get("sunday"));
        int num = tbScheduleService.updatesunday(account, sunday);
        if (num == 0) {
            return new JsonResult(-1, "请求失败！");
        } else {
            System.out.println(new JsonResult(num, "请求成功"));
            return new JsonResult(num);
        }
    }



    @RequestMapping("/queryweekly")
    @ResponseBody
    public JsonResult queryweekly(@RequestBody JSONObject jsonObject){
        String account=String.valueOf(jsonObject.get("account"));
        List<TbWeeklyschedule> list=tbWeeklyscheduleService.queryweekly(account);
        if (list.size()==0){
            return new JsonResult(-1,"请求失败！");
        }else {
            System.out.println(new JsonResult(list,"请求成功"));
            return new JsonResult(list);
        }
    }

    @RequestMapping("/querySelectDoc")
    @ResponseBody
    public JsonResult querySelectDoc(@RequestBody JSONObject jsonObject) {
        System.out.println(jsonObject);
        String department=String.valueOf(jsonObject.get("department"));
        String selectday=String.valueOf(jsonObject.get("selectday"));
        List<TbUser> list=new ArrayList<>();
        switch (selectday){
            case "0":list= docOndutyService.selectSundayDoc(department);break;
            case "1":list= docOndutyService.selectMondayDoc(department);break;
            case "2":list= docOndutyService.selectTuesdayDoc(department);break;
            case "3":list= docOndutyService.selectWednesayDoc(department);break;
            case "4":list= docOndutyService.selectThursdayDoc(department);break;
            case "5":list= docOndutyService.selectFridayDoc(department);break;
            case "6":list= docOndutyService.selectSaturdayDoc(department);break;
        }
        if (list.size() == 0) {
            return new JsonResult(-1, "请求失败，没有查询到医生信息！");
        } else {
            return new JsonResult(list, "请求成功");
        }
    }
}
